package com.java.comprehensive.arithmetic.leecode.tags.linkedlist;

import util.ListNode;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: 窦绍飞
 * @Date: 2022/7/6
 */
public class LeetCode_160 {

	public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
		if (headA == null || headB == null) return null;
		ListNode A = headA, B = headB;
		while (A != B) {
			A = A == null ? headB : A.next;
			B = B == null ? headA : B.next;
		}
		return A;
	}

	public ListNode getIntersectionNodeTwo(ListNode headA, ListNode headB) {
		Set<ListNode> visited = new HashSet<>();
		ListNode temp = headA;
		while (temp != null) {
			visited.add(temp);
			temp = temp.next;
		}
		temp = headB;
		while (temp != null) {
			if (visited.contains(temp)) {
				return temp;
			}
			temp = temp.next;
		}
		return null;
	}

	public static void main(String[] args) {

	}
}
